import json
import requests


def queue_rpush(redis, queue_id, url: str, forward_urls: list, depth: int):
    """
    右入队列
    """
    redis.rpush(queue_id, json.dumps({
        'url': url,
        'forward_urls': forward_urls,
        'depth': depth,
    }))


def queue_lpush(redis, queue_id, url: str, forward_urls: list, depth: int):
    """
    左入队列
    """
    redis.lpush(queue_id, json.dumps({
        'url': url,
        'forward_urls': forward_urls,
        'depth': depth,
    }))


def queue_lpop(redis, queue_id) -> [dict | None]:
    """
    左取队列项
    """
    data = redis.lpop(queue_id)
    if data:
        return json.loads(data)
    return None


def queue_rpop(redis, queue_id) -> [dict | None]:
    """
    右取队列项
    """
    data = redis.rpop(queue_id)
    if data:
        return json.loads(data)
    return None
